package fr.gest4team.app.pilotage.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.gest4team.app.pilotage.metier.TacheOm;
import fr.gest4team.app.pilotage.metier.VersionOm;
import fr.gest4team.app.pilotage.service.ServiceTache;
import fr.gest4team.app.pilotage.service.ServiceVersion;
import fr.gest4team.app.pilotage.service.exceptions.ServiceVersionException;

/**
 * Servlet implementation class TacheServlet
 */
public class TacheServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TacheServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
        IOException {
        execute(request, response);
    }

    private void execute(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
        IOException {
        String dispatcher = "";
        final String action = request.getParameter("action");
        try {
            request.setAttribute("LISTE-VERSION", chargerListeVersion());
            if (action == null || "lister-all".equalsIgnoreCase(action)) {
                final List<TacheOm> liste = ServiceTache.getInstance().chargerTaches();
                request.setAttribute("TACHES", liste);
                dispatcher = "/taches.jsp";
            } else if ("lister".equals(action)) {
                final String version = request.getParameter("idversion");
                final VersionOm versionOm = ServiceVersion.getInstance().chargerVersion(Integer.parseInt(version));
                final List<TacheOm> list = new ArrayList<TacheOm>();
                list.addAll(versionOm.getTaches());
                request.setAttribute("IDVERSION", versionOm.getIdVersion());
                request.setAttribute("VERSION", versionOm.getNom());
                request.setAttribute("TACHES", list);
                dispatcher = "/taches.jsp";
            } else if ("change-version".equals(action)) {

                final String version = request.getParameter("version");
                final String idtache = request.getParameter("idtache");
                final TacheOm tache = ServiceTache.getInstance().chargerTache(Integer.parseInt(idtache));
                VersionOm versionOm = ServiceVersion.getInstance().chargerVersion(version);
                tache.setVersion(versionOm.getIdVersion());
                ServiceTache.getInstance().majTache(tache);

                final String idversion = request.getParameter("idversion");
                if (idversion == null || "".equals(idversion) || "null".equals(idversion)) {
                    final List<TacheOm> liste = ServiceTache.getInstance().chargerTaches();
                    request.setAttribute("TACHES", liste);
                } else {
                    versionOm = ServiceVersion.getInstance().chargerVersion(Integer.parseInt(idversion));
                    final List<TacheOm> list = new ArrayList<TacheOm>();
                    list.addAll(versionOm.getTaches());
                    request.setAttribute("TACHES", list);
                    request.setAttribute("IDVERSION", versionOm.getIdVersion());
                    request.setAttribute("VERSION", versionOm.getNom());
                }
                dispatcher = "/taches.jsp";
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
        final RequestDispatcher rd = request.getRequestDispatcher(dispatcher);
        rd.forward(request, response);

    }

    private List<String> chargerListeVersion() throws ServiceVersionException {
        return ServiceVersion.getInstance().chargerListeVersion();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
        execute(request, response);
    }

}
